const UserModel = require("../../models/user/UserModel.js");
const getOnlyId = () => {
  let random = Math.random().toString(16).substring(8);
  return random;
};
// 0代表所有权限全开 1 的权限只有查看首页信息
const roleList = {
  // 最终数据全部是放在main中,可以省略，只处理二级路由和三记路由
  0: [
    {
      id: getOnlyId(),
      name: "系统管理",
      path: "intro", //一级路由path
      component: "Index.vue",
      icon: "Monitor",
      children: [
        {
          // 重定向
          path: "/main/intro",
          redirect: "/main/intro/overview",
        },
        {
          id: getOnlyId(),
          name: "核心技术",
          path: "overview", //二级路由path
          component: "Overview.vue",
        },
        {
          id: getOnlyId(),
          name: "商品统计",
          path: "dashboard", //二级路由
          component: "Dashboard.vue",
        },
      ],
    },
    {
      id: getOnlyId(),
      name: "系统设计",
      path: "system", //一级路由path
      component: "Index.vue",
      icon: "Setting",
      children: [
        {
          path: "/main/system",
          redirect: "/main/system/user",
        },
        {
          id: getOnlyId(),
          name: "用户管理",
          path: "user", //二级路由path
          component: "User.vue",
        },
        {
          id: getOnlyId(),
          name: "部门管理",
          path: "depart", //二级路由
          component: "Depart.vue",
        },
        {
          id: getOnlyId(),
          name: "菜单管理",
          path: "menu", //二级路由
          component: "Menu.vue",
        },
        {
          id: getOnlyId(),
          name: "角色管理",
          path: "role", //二级路由
          component: "Role.vue",
        },
      ],
    },
    {
      id: getOnlyId(),
      name: "商品中心",
      path: "goods", //一级路由path
      component: "Index.vue",
      icon: "Goods",
      children: [
        {
          path: "/main/goods",
          redirect: "/main/goods/classify",
        },
        {
          id: getOnlyId(),
          name: "商品类别",
          path: "classify", //二级路由path
          component: "Classify.vue",
        },
        {
          id: getOnlyId(),
          name: "商品信息",
          path: "info", //二级路由
          component: "Info.vue",
        },
      ],
    },
    {
      id: getOnlyId(),
      name: "更多信息",
      path: "tale", //一级路由path
      component: "Index.vue",
      icon: "ChatDotRound",
      children: [
        {
          path: "/main/tale",
          redirect: "/main/tale/stories",
        },
        {
          id: getOnlyId(),
          name: "你的故事",
          path: "stories", //二级路由path
          component: "Stories.vue",
        },
        {
          id: getOnlyId(),
          name: "故事列表",
          path: "talelist", //二级路由
          component: "Talelist.vue",
        },
      ],
    },
  ],
  1: [
    {
      id: getOnlyId(),
      name: "系统管理",
      path: "intro", //一级路由path
      component: "Index.vue",
      icon: "Monitor",
      children: [
        {
          // 重定向
          path: "/main/intro",
          redirect: "/main/intro/overview",
        },
        {
          id: getOnlyId(),
          name: "核心技术",
          path: "overview", //二级路由path
          component: "Overview.vue",
        },
        {
          id: getOnlyId(),
          name: "商品统计",
          path: "dashboard", //二级路由
          component: "Dashboard.vue",
        },
      ],
    },
    {
      id: getOnlyId(),
      name: "更多信息",
      path: "tale", //一级路由path
      component: "Index.vue",
      icon: "ChatDotRound",
      children: [
        {
          path: "/main/tale",
          redirect: "/main/tale/stories",
        },
        {
          id: getOnlyId(),
          name: "你的故事",
          path: "stories", //二级路由path
          component: "Stories.vue",
        },
        {
          id: getOnlyId(),
          name: "故事列表",
          path: "talelist", //二级路由
          component: "Talelist.vue",
        },
      ],
    },
  ],
};

const UserDB = {
  async login({ username, md5pswsolt }) {
    return UserModel.find({ username, md5pswsolt });
  },
  // 获取用户菜单权限
  async getUserMenu({ _id, role }) {
    try {
      let res = await UserModel.find({ _id });
      if (res.length === 0) {
        // 查询用户不存在
        return false;
      } else {
        return roleList[role];
      }
    } catch (error) {}
  },
  // 获取用户列表
  async getUserList(params) {
    let { page, pageSize } = params; // 页码,每页条数
    page = +page;
    pageSize = +pageSize;
    let skip = (page - 1) * pageSize;

    let config = {};
    if (params?.realname)
      config.realname = { $regex: new RegExp(params.realname, "gi") }; // mongoose中的正则操作符$regex
    if (params?.username) {
      config.username = { $regex: new RegExp(params.username, "gi") };
    }

    // 区域参数中只有页码和页大小的情况，还有传递了但是为空字符串的情况需要排除，然后只针对数组情况进行处理
    if (params?.createtime && params?.createtime?.length !== 0) {
      // 需要注意查询条件的处理
      // console.log(params); //[ '2023-10-04 00:00:00', '2023-11-24 00:00:00' ]
      // console.log(Date.parse(params.createtime[0]));
      config.createtime = {
        $gte: +Date.parse(params.createtime[0]),
        $lte: +Date.parse(params.createtime[1]),
      };
    }

    if (params?.phonenumber) {
      // 校验手机号为数字
      let isValid = /^\d+$/.test(params.phonenumber);
      // 前端需要处理手机号格式校验，后端也需要进行处理
      if (isValid) {
        config.phonenumber = { $regex: new RegExp(params.phonenumber, "g") };
      }
    }
    if (params?.state) {
      let state = +params.state;
      config.state = state;
    }

    return UserModel.find({ ...config })
      .skip(skip)
      .limit(pageSize);
  },
  async getUserListCount() {
    return UserModel.find().countDocuments();
  },
  async deleteUserListItem(id) {
    return UserModel.deleteOne({ _id: id });
  },
  async addUserList(params) {
    return UserModel.create(params);
  },
  async updateUserInfo(id, params) {
    return UserModel.updateOne({ _id: id }, { ...params });
  },
};

module.exports = UserDB;
